package com.liming.admin.service.impl;

import com.liming.admin.service.CouponService;
import com.liming.common.pojo.dto.coupon.CouponPageQuery;
import com.liming.common.pojo.vo.PageVO;
import com.liming.common.pojo.vo.coupon.openfeign.FeignCouponVO;
import com.liming.common.pojo.vo.coupon.web.CouponVO;
import com.liming.coupon.client.CouponAdminClient;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
 * @author liming
 * @date 2025/1/19
 */
@Service
@AllArgsConstructor
public class CouponServiceImpl implements CouponService {

    private final CouponAdminClient couponAdminClient;

    @Override
    public PageVO<CouponVO> page(CouponPageQuery query) {
        PageVO<FeignCouponVO> feignResult = couponAdminClient.page(query);
        PageVO<CouponVO> result = new PageVO<>();
        // 拷贝除records外的属性 拷贝了的话 里面的records的属性为 FeignCouponVO
        BeanUtils.copyProperties(feignResult, result, "records");
        result.setRecords(feignResult.getRecords().stream().map(feign -> {
            CouponVO mvo = new CouponVO();
            BeanUtils.copyProperties(feign, mvo);
            return mvo;
        }).toList());
        return result;
    }

    @Override
    public CouponVO get(Long id) {
        FeignCouponVO result = couponAdminClient.get(id);
        CouponVO vo = new CouponVO();
        BeanUtils.copyProperties(result, vo);
        return vo;
    }
}
